import React, { Component, Fragment } from "react";
// antd
import { Modal, Table, Button, Col, Pagination, Form, Input, message } from "antd";
import Action from "@c/buttonAction/Index";
// http
import http from '../../../utils/request';


class CloudManage extends Component {
    constructor(props){
        super(props);
        this.state = {
            // 表格分页
            total: '',
            page: 1,
            page_number: 10,
            dataSource: [],
            columns: [
                {title: "平台名称", dataIndex: "name", key: "name"},
                {title: "平台code", dataIndex: "code", key: "code"},
                {title: "操作", dataIndex: "operation", key: "operation", 
                    render: (text, rowData) => {
                        return (
                            <div className="inline-button">
                                <Action>
                                    {/* <Action.Button onClick={() => this.onEditCloudBtn({rowData: rowData, status: true})}>编辑</Action.Button> */}
                                    <Action.Button onClick={() => this.onDeleteCloudPlatformBtn(rowData)}>删除</Action.Button>
                                </Action>
                            </div>

                        )
                    }           
                }
            ],
            loading: false,
            visible_add: false,     // 添加
            visible_edit: false,    // 编辑
            visible_delete: false,  // 删除
            visible_edit_id: '',
            visible_edit_name: '',
            visable_edit_code: '',
            cloud_platform_id: '',
            cloud_platform_name: '',
            search_cloud_platform_name: '',

        }
        this.formRefAdd = React.createRef();
        this.formRefEdit = React.createRef();
        this.formRefMsg = React.createRef();
    }

    componentDidMount() {
        this.loadData()
    }

    loadData = () => {
        this.setState({
            loading: true
        })
        const requestData = {
            'page': this.state.page,
            'limit': this.state.page_number,
            'name__contains': this.state.search_cloud_platform_name,
        }

        // 获取分组列表
        http.get('/cloud/platform', {params: requestData}).then(response=>{
            this.setState({
                dataSource: response.data.data.items,
                total: response.data.data.pages.total,
                loading: false
            })
        }).catch()
    }

    /** 切换每页数量 */
    onChangeSizePage = (page, page_number) => {
        this.setState({
            page: page,
            page_number: page_number,
        }, () => {
            this.loadData()
        })
    }

    /** 云平台搜索 */
    onFinishSearch = (data) => {
        this.setState({
            page: 1,
            search_cloud_platform_name: data.name ? data.name : null,
        })
        this.loadData()
    }

    /** 添加云平台按钮 */
    onAddCloudPlatformBtn = () => {
        this.setState({
            visible_add: true
        })
    }

    /** 添加云平台取消 */
    onFinishAddCancel = () => {
        this.setState({
            visible_add: false
        })
        this.formRefAdd.current.resetFields()
    }

    /** 添加云平台提交 */
    onFinishAdd = (data) => {
        const requestData = {
            name: data.name,
            code: data.code
        }
        http.post('/cloud/platform', requestData).then(response=>{
            message.success(response.data.msg)
            this.setState({
                visible_add: false,
            })
            this.formRefAdd.current.resetFields()
            this.loadData()
        }).catch()
    }

    /** 编辑云平台按钮 */
    onEditCloudBtn = (data) => {
        http.get('/cloud/platform').then(response=> {
            this.formRefEdit.current.setFieldsValue({
                id: data.rowData.id,
                name: data.rowData.name,
            })
        })
        
        this.setState({
            visible_edit: true,
        })
    }

    /** 编辑云平台取消 */
    onFinishEditCancel = () => {
        this.setState({
            visible_edit: false,
        })
        this.formRefEdit.current.resetFields();
    }

    /** 编辑云平台提交 */
    onFinishEdit = (data) => {
        const requestData = {
            name: data.name,
        }
        http.put('/cloud/platform/' + data.id, requestData).then(response=>{
            message.success(response.data.msg)
            this.setState({
                visible_edit: false
            })
            this.formRefEdit.current.resetFields();
            this.loadData()
        }).catch()
    }

    /** 删除云平台提交 */
    handleCloudPlatformDelete = () => {
        http.delete('/cloud/platform/' + this.state.cloud_platform_id).then(response=>{
            message.success(response.data.msg)
            this.setState({
                visible_delete: false,
            })
            this.loadData()
        }).catch()    
    }

    /** 删除云平台按钮 */
    onDeleteCloudPlatformBtn = (data) => {
        this.setState({
            cloud_platform_id: data.id,
            cloud_platform_name: data.name,
            visible_delete: true
        })
    }

    render(){
        const { loading, dataSource, columns, page, total, visible_add, visible_edit, visible_delete, cloud_platform_name } = this.state
        const rowSelection = {
            onChange: this.onCheckbox
        }
        var myStyle = {margin: '20px'}
        var myStyle1 = {margin: '10px', float: 'right'}
        return (
            <Fragment>
                <Form layout="inline" onFinish={this.onFinishSearch}>
                    <Form.Item label="云平台名称" name="name">
                        <Input placeholder="请输入云平台名称"  />
                    </Form.Item>
                    <Form.Item shouldUpdate={true}>
                        <Button type="primary" htmlType="submit">搜索</Button>
                    </Form.Item>
                </Form>
                
                <div style={myStyle1}><Button type="primary" onClick={this.onAddCloudPlatformBtn}>添加云平台</Button></div>
                <Table
                    pagination={false} 
                    rowSelection={rowSelection} 
                    rowKey="id" 
                    columns={columns} 
                    dataSource={dataSource}
                    loading={loading}
                    scroll={{ y: 500, x: '70vw' }}
                >
                </Table>

                <div style={myStyle}>
                    <Col span={16}>
                        <Pagination
                            onChange={this.onChangeSizePage}
                            current={page}
                            total={total}
                            showSizeChanger
                            showQuickJumper
                            showTotal={total => `Total ${total} items`}
                        />
                    </Col>
                </div>

                <Modal
                    title="添加云平台"
                    visible={visible_add}
                    onCancel={this.onFinishAddCancel}
                    okText="确认"
                    cancelText="取消"
                    footer={null}
                >
                    <Form
                        onFinish={this.onFinishAdd}
                        ref={this.formRefAdd}
                    >
                        <Form.Item label="云平台名称" name="name" rules={[{ required: true}]}>
                            <Input></Input>
                        </Form.Item>

                        <Form.Item label="云平台代码" name="code" rules={[{ required: true}]}>
                            <Input></Input>
                        </Form.Item>

                        <Form.Item>
                            <Button htmlType="submit" type="primary">确定</Button>
                        </Form.Item>                                                               
                    </Form>           
                </Modal>

                <Modal
                    title="编辑云平台"
                    visible={visible_edit}
                    onCancel={this.onFinishEditCancel}
                    okText="确认"
                    cancelText="取消"
                    footer={null}
                >
                    <Form
                        onFinish={this.onFinishEdit}
                        ref={this.formRefEdit}
                    >
                        <Form.Item label="云平台ID" name="id" hidden>
                            <Input ></Input>
                        </Form.Item>
                        <Form.Item label="云平台名称" name="name" rules={[{ required: true}]}>
                            <Input disabled></Input>
                        </Form.Item>
                        <Form.Item>
                            <Button htmlType="submit" type="primary">确定</Button>
                        </Form.Item>                                                               
                    </Form>           
                </Modal>

                <Modal
                    title="删除云平台"
                    visible={visible_delete}
                    onOk={this.handleCloudPlatformDelete}
                    onCancel={() => {this.setState({visible_delete: false, cloud_platform_id: ''})}}
                    okText="确认"
                    cancelText="取消"
                >
                    <p>确定删除云平台<span style={{color: 'red'}}> {cloud_platform_name}</span>? 删除后将无法恢复</p>
                </Modal>
            </Fragment>
        )
    }
}
export default CloudManage;